package com.ccl.iot.device;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;

import com.ccl.iot.dvr.utils.CommonUtils;
import com.ccl.iot.template.GraphicView;
import com.ccl.iot.device.DeviceControler.OnBitmapChangeListener;
import com.ccl.iot.views.WaitRegView;

public class DeviceControlView extends GraphicView {
	private static final int KFILL_WIDTH = 3;
	private static final int KFILL_HEIGHT = 3;
	
	/*
	 * 	Non-static functions
	 */		
	private Size iViewSize = new Size();
	private Size iFillSize = new Size();
	private Paint iPaint;
	
	private Object iUpdateLock = new Object();
	private IOTDevice iDevice;
	private DeviceControler iControler;
		
	public DeviceControlView(Context aContext) {
		super(aContext);
		
		initialize(aContext);
	}
	
	private void initialize(Context aContext){
		DisplayMetrics tMetrics = aContext.getResources().getDisplayMetrics();
		
		if (tMetrics != null){
			iFillSize.iWidth = (int) (KFILL_WIDTH * tMetrics.xdpi * (1.0f / 72));
			iFillSize.iHeight = (int) (KFILL_HEIGHT * tMetrics.ydpi * (1.0f / 72));
		}
		
	}
	
	@Override
	protected void onShow(){
		super.onShow();
		
		synchronized(iUpdateLock){
			if (iControler != null){
				iControler.setOnBitmapChangeListener(iBitmapChangeListener);
			}
		}
	}
	
	@Override
	protected void onHide(){
		super.onHide();
		
		synchronized(iUpdateLock){
			if (iControler != null){
				iControler.setOnBitmapChangeListener(null);
			}
		}
	}
	
	private void resize(){
		if (iControler != null){
			iControler.setPadding(iFillSize.iWidth, iFillSize.iHeight, iFillSize.iWidth, iFillSize.iHeight);
			iControler.resize(iViewSize.iWidth, iViewSize.iHeight);
		}
	}
	@Override
	public void invalidate() {
		resize();
		
		super.invalidate();
	}

	@Override
	public void postInvalidate() {
		resize();
		
		super.postInvalidate();
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder aHolder, int aFormat, int aWidth, int aHeight) {
		if (aWidth > 0 && aHeight > 0){
			iViewSize.set(aWidth, aHeight);
			
			resize();
			
			if (iPaint == null){
				iPaint = new Paint();
			}
		}
		
		super.surfaceChanged(aHolder, aFormat, aWidth, aHeight);
	}
	
	public void setDevice(IOTDevice aDevice){
		synchronized(iUpdateLock){
			
			if(aDevice instanceof BatchIntentionDevice){
			}
			
			if (aDevice != iDevice){
				if (iControler != null){
					iControler.setOnBitmapChangeListener(null);
				}
				
				iDevice = aDevice;
				if (iDevice != null){
					iControler = DeviceControler.BuildControler(iDevice);
					if (iControler != null){
						iControler.setBackColor(Color.WHITE);
						iControler.setOnBitmapChangeListener(iBitmapChangeListener);
						
						resize();
					}
				}else{
					iControler = null;
				}
			}
		}
		
		super.postInvalidate();
	}
	
	private OnBitmapChangeListener iBitmapChangeListener = new OnBitmapChangeListener(){
		@Override
		public void onBitmapChange() {
			CommonUtils.hideWaitRegView(mWaitRegView);
			DeviceControlView.this.postInvalidate();
		}
	};
	
	@Override
	protected void onPaint(Canvas aCanvas){
		if (aCanvas != null){
			Bitmap tBitmap = null;
			
			synchronized(iUpdateLock){
				if (iControler != null){
					tBitmap = iControler.getBitmap(this);
				}
			}
			
			if (tBitmap != null){
				aCanvas.drawBitmap(tBitmap, 0, 0, iPaint);
//				tBitmap.recycle();
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (iControler != null){
			CommonUtils.showWaitRegView(mWaitRegView);
			return iControler.processTouchEvent(event);
		}else{
			return super.onTouchEvent(event);
		}
	}

	private WaitRegView mWaitRegView;

	public void setWaitRegView(WaitRegView waitRegView){
		mWaitRegView = waitRegView;
	}
}
